<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加</title>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}"/>
    <script th:src="@{/component/layui/layui.js}"></script>
    <script th:src="@{/component/pear/pear.js}"></script>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <input type="hidden" id="userId" name="userId" th:value="${userInfo.userId}">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span style="color: red">*</span>用户名
                </label>
                <div class="layui-input-block">
                    <input type="text" id="userName" th:value="${userInfo.userName}" name="userName"
                           lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span style="color: red">*</span>昵称</label>
                <div class="layui-input-block">
                    <input type="text" id="nickName" th:value="${userInfo.nickName}" name="nickName" required=""
                           placeholder="请输入昵称" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    电话号码
                </label>
                <div class="layui-input-block">
                    <input type="text" name="phone" th:value="${userInfo.phone}" autocomplete="off"
                           placeholder="请输入电话号码" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    email
                </label>
                <div class="layui-input-block">
                    <input type="text" name="email" th:value="${userInfo.email}" autocomplete="off"
                           placeholder="请输入email" class="layui-input">
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>

                <div class="layui-input-block">
                    <select name="roleId"  id="roleId" lay-verify="required">
                        <option value="-1">----请选择----</option>
                        <option value="1" th:value="${roleInfo.roleId}" th:each="roleInfo:${roles}" th:selected="${roleInfo.roleId == userInfo.roleId}">[[${roleInfo.roleName}]]
                        </option>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'jquery', 'laydate'], function () {
        let form = layui.form;
        $ = layui.jquery;
        let laydate = layui.laydate;

        //渲染日期组件
        laydate.render({
            elem: '#date' //指定元素
        });

        form.on('submit(user-save)', function (data) {
            new Promise(resolve => {
                $.ajax({
                    url: '/sys/user',
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        resolve(result)
                    }
                })
            }).then(value => {
                if (value.success) {
                    layer.msg(value.msg, {icon: 1, time: 1000}, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.location.reload();
                    });
                } else {
                    layer.msg(value.msg, {icon: 2, time: 1000});
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>